// 导入全局Api管理
import { GET_ARTICLE_DETAIL } from "@/assets/api/api.js";

// 导入Axios拦截器
import instance from "@/axios_instance/axios_instance.js";

// 导入文章评论公共组件
const Comment = resolve =>
  require(["@/components/comment/comment.vue"], resolve);

export default {
  name: "ArticleContent",
  components: {
    Comment
  },
  data() {
    return {
      // 初始化文章主要内容
      articleDetail: {},
      // 初始化文章文字段落
      contentArr: [],
      // 初始化文章图片合集
      picArr: [],
      // viewer 配置项
      viewOptions: {
        url: "data-source"
      },
    };
  },
  mounted() {
    // 获取文章详情
    this.getArticleDetail();
  },
  methods: {
    // 获取文章详情的方法
    getArticleDetail() {
      let url = GET_ARTICLE_DETAIL + "/" + this.$route.query.id;
      instance.get(url).then(res => {
        if (res.status == 200) {
          this.articleDetail = res.data;

          // 处理文章内容
          this.handlingArticleContent();
        }
      });
    },

    // 处理文章内容的方法
    handlingArticleContent() {
      let content = this.articleDetail.mainText.split("\n");
      let splitIndex = 0;
      content.forEach((item, index) => {
        if (item === "本期作品：") {
          splitIndex = index;
        }
      });

      this.contentArr = content.slice(0, splitIndex);
      this.picArr = content.slice(splitIndex + 1, content.length);
    }
  }
};
